/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package abogados;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Conexion {

    private Connection conexion;
    
       public Conexion() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conexion = DriverManager.getConnection("jdbc:mysql://localhost/solucionesjuridicas", "root", "root");
        } catch (Exception e) {
            System.out.println("Imposible realizar conexion con la BD");
            e.printStackTrace();
        }
    }

    public void crearTabla() {
        try {

            Statement st = conexion.createStatement();
            st.executeUpdate("CREATE TABLE clientes (claveElector VARCHAR(20), PRIMARY KEY(claveElector), nombre VARCHAR(20), apellidos VARCHAR(20), domicilio VARCHAR(20), ciudad VARCHAR(20),telefono VARCHAR(20), correo VARCHAR(20), comentarios VARCHAR(1000))");

        } catch (SQLException ex) {
            Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void insertar(String clave, String nom, String ape, String dom, String ciu, String tel, String corr, String com) {
        try {

            Statement st = conexion.createStatement();
            st.executeUpdate("INSERT INTO clientes (claveElector, nombre, apellidos, domicilio, ciudad, telefono, correo, comentarios) VALUES('" + clave + "','" + nom + "','" + ape + "','" + dom + "','" + ciu + "','" + tel + "','" + corr + "','" + com + "' )");

        } catch (SQLException ex) {
            Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void manipular(String com) {
        try {
            Statement st = conexion.createStatement();
            st.executeUpdate("INSERT INTO clientes (comentarios)VALUES('" + com + "' )");
        } catch (SQLException ex) {
            Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void consulta() {
        try {
            Statement st = conexion.createStatement();
            ResultSet rs = st.executeQuery("SELECT claveElector FROM clientes");

            while (rs.next()) {

                System.out.println("clave = " + rs.getObject("claveElector"));
                //System.out.println("cont=" + rs.getObject("contrasena"));
            }
            rs.close();

        } catch (SQLException ex) {
            Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

 

    public Connection getConexion() {
        return conexion;
    }

    public void cerrar(ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (Exception e) {
                System.out.print("No es posible cerrar la Conexion");
            }
        }
    }

    public void cerrar(java.sql.Statement stmt) {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (Exception e) {
            }
        }
    }

    public void destruir() {

        if (conexion != null) {

            try {
                conexion.close();
            } catch (Exception e) {
            }
        }
    }
}
